package com.huawei.csi.tool.ui.cell;


import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;
import com.huawei.csi.tool.domain.CJColor;
import com.huawei.csi.tool.domain.CJLinearGradient;
import com.huawei.csi.tool.domain.CJStop;
import com.huawei.csi.tool.domain.Project;
import com.huawei.csi.tool.util.FileUtil;
import com.huawei.csi.tool.util.OSUtil;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.text.Text;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lxw
 */
public class ProjectCell extends ListCell<Project> {
    public static final List<LinearGradient> LIST;

    public static final List<String> COMMON_WAREHOUSE;

    static {
        Optional<Object> optional = FileUtil.jsonFile2Class("data/linear_gradient.json", new TypeToken<List<CJLinearGradient>>() {
        }.getType());
        if (optional.isPresent()) {
            List<CJLinearGradient> cjLinearGradients = (List<CJLinearGradient>) optional.get();
            LIST = cjLinearGradients.stream().map(ProjectCell::convertToCJPatin).collect(Collectors.toList());
        } else {
            LIST = new ArrayList<>();
        }
        COMMON_WAREHOUSE = Lists.newArrayList("github", "gitee", "gitcode");
    }

    private final Text nameText;
    private final Label descLabel;
    private final BorderPane pane;
    private final StackPane regionWrapper;

    public ProjectCell() {
        Region region = new Region();
        region.setPrefSize(14, 14);
        region.setMaxSize(14, 14);
        regionWrapper = new StackPane(region);
        regionWrapper.getStyleClass().add("url-btn");
        regionWrapper.setPrefSize(18, 18);
        regionWrapper.setMaxSize(18, 18);
        nameText = new Text();
        nameText.getStyleClass().add("name-text");

        regionWrapper.setOnMousePressed(this::onClickGotoGithubBtn);
        Region region1 = new Region();
        HBox.setHgrow(region1, Priority.ALWAYS);
        HBox.setMargin(regionWrapper, new Insets(0, 4, 0, 0));
        HBox box = new HBox(nameText, region1, regionWrapper);
        box.setAlignment(Pos.CENTER_LEFT);
        descLabel = new Label();
        descLabel.getStyleClass().add("small-font");
        descLabel.getStyleClass().add("desc-label");
        descLabel.setWrapText(true);

        pane = new BorderPane();
//        pane.getStyleClass().add("project-pane");
        pane.setTop(box);
        pane.setCenter(descLabel);

        BorderPane.setAlignment(descLabel, Pos.TOP_LEFT);
        pane.setPrefSize(220, 70);
        pane.setMinSize(220, 70);
        setPrefWidth(220);
    }

    public void onClickGotoGithubBtn(MouseEvent event) {
        if (getItem() == null) {
            return;
        }
        OSUtil.showDoc(getItem().getUrl());
    }

    @Override
    protected void updateItem(Project item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setText("");
            setGraphic(null);
            return;
        }

        if (isSelected()) {
            nameText.setFill(Color.WHITE);
        } else {
            nameText.setFill(LIST.get(new Random().nextInt(26)));
        }
        nameText.setText(item.getName());
        descLabel.setText((OSUtil.isEnglish() ? item.getDescEn() : item.getDescZh()));
        String type = item.getType().toLowerCase(Locale.ROOT);
        if (COMMON_WAREHOUSE.contains(type)) {
            regionWrapper.getStyleClass().setAll("url-btn", type + "-btn");
        } else {
            regionWrapper.getStyleClass().setAll("url-btn", "webpage-btn");
        }
        setGraphic(pane);
    }

    /***
     * CJLinearGradient
     *
     * @param gradient 渐变
     * @return javafx.scene.paint.LinearGradient
     **/
    public static LinearGradient convertToCJPatin(CJLinearGradient gradient) {
        List<CJStop> cjStopList = gradient.getStops();
        Stop[] stops = new Stop[cjStopList.size()];
        for (int i = 0; i < cjStopList.size(); i++) {
            CJStop cjStop = cjStopList.get(i);
            CJColor cjColor = cjStop.getCjColor();
            stops[i] = new Stop(cjStop.getOffset(), new Color(cjColor.getRed(), cjColor.getGreen(), cjColor.getBlue(), cjColor.getOpacity()));
        }
        return new LinearGradient(gradient.getStartX(), gradient.getStartY(),
                gradient.getEndX(), gradient.getEndY(), true, CycleMethod.NO_CYCLE, stops);
    }

}
